// Creating the Sample 2D Array, to add items in, just test[last] = ["yoo","hehe","lala"]
var search = [];
var order = [];
var loan = [];
// 3 x 3 array
//TYPE =  1 - create, 2 - noify, 3 - inventory , 4 - others

//Text filter, with LIKE '%chris ahhh%'

function filter(ele_text,type){
    var searchValue = ele_text.toLowerCase();
    var counter = 0;
    var filteredArray = [];
    var tempArray = [];
    switch(type){
        case 1:
            tempArray = search;
            break;
        case 2:
            tempArray = order;
            break
        case 3:
            tempArray = loan;
            break;
    }
    clearTable(tagId); // remove the previous table
    //filter thru name First
    for (var z=0; z <tempArray.length; z++){
        if ((tempArray[z][0].toLowerCase()).match(".*"+searchValue+".*")){  // Filter thru the list of name in test array
            filteredArray[counter] = tempArray[z];
            counter++;
        }
    }
    dynamicTable(filteredArray,type);
}

function clearTable(tagId){
    document.getElementById(tagId).innerHTML = "";
}
//TYPE =  1 - create, 2 - noify, 3 - inventory , 4 - others
// (Array data, tagId - the Div,  type - 1,2,3,4
function dynamicTable(filteredArray,type){
    var root="";
    var tab=document.createElement('table');
    tab.setAttribute("id","inventory_table");
    var tbo=document.createElement('tbody');
    var row, cell;
    var headerTitle = [];
    if(filteredArray == "all"){
        switch(type){
            case 1:
                filteredArray = search;
                break;
            case 2:
                filteredArray = order;
                break;
            case 3:
                filteredArray = loan;
                break;
        }
    }
    switch(type){
        case 1:
            root = document.getElementById('inventory_table_wrapper');
            headerTitle = ["Name","Weird Number","Roles"];
            break;
        case 2:
            root = document.getElementById('inventory_table_wrapper');
            headerTitle = ["Id","Weird","Laugh","Pui"];
            break;
        case 3:
            root = document.getElementById('inventory_table_wrapper');
            headerTitle =  ["Ding","Dong","Pong","Ping"];
            break;
   
    }
    //Generating the Table Headers
    row=document.createElement('tr');
    row.setAttribute("class","inventory_table_row");
    for(var j=0;j<headerTitle.length;j++){
        cell=document.createElement('td');
        cell.setAttribute("class","inventory_table_title");
        cell.appendChild(document.createTextNode(headerTitle[j]));
        row.appendChild(cell);
    }
    tbo.appendChild(row);

    //Generating Table data
    for(var i=0;i<filteredArray.length;i++){  // loop total number of objects in the array, 1D
        row=document.createElement('tr');
        row.setAttribute("class","inventory_table_row");
        for(var v=0;v<filteredArray[i].length;v++){  // loop total number of objects in the array, 2D
            cell=document.createElement('td');
            cell.setAttribute("class","inventory_table_title");
            cell.appendChild(document.createTextNode(filteredArray[i][v]));
            row.appendChild(cell);
        }
        tbo.appendChild(row);
    }
    tab.appendChild(tbo);
    root.appendChild(tab);
}

function displayAll(type){
    generateInitialData();
    clearTable();
    var tempArray= [];
    switch(type){
        case 1:
            tempArray = search;
            break;
        case 2:
            tempArray = order;
            break;
        case 3:
            tempArray = loan;
            break;

    }
    dynamicTable(tempArray,type);
}

function generateInitialData(){
    search = [["Chua", 65531438,"Manager"],["Chris",12345678,"ProjectLeader"],["Simon",1231228,"Creative"],["Livy",23413228,"Business"]];
    order = [[1, "outofstock","action",2],[2, "available","boo",3],[3, "low in stock","wee",5],[5, "outofstock","buy more",6]];
    loan = [["yoyo", 65531438,"hehe","haha"],["keke",12345678,"kaka","tete"],["nana",1231228,"nini","hoho"],["lolol",23413228,"fefe","heta"]];
    
}

// put in the form name and the type , 1 - create, 2 - noify, 3 - inventory , 4 - others

// this function will only take any information off the inputs text.
// the value is taken 1 by 1 base on the arrangement of the input tags (By Lines)
function addInputs(formId,type){
    var temp = [];
    var counter = 0;
    var fields = formId.getElementsByTagName("input");
    for(var i = 0; i<fields.length;i++){
        if(fields[i].type == 'text'){
            temp[counter] = fields[i].value;
            counter++;
        }
    }
    switch(type){
        case 1:
            create[create.length] = temp;
            break;
        case 2:
            notification[notification.length] = temp;
            dynamicTable(notification,'inventory_table_wrapper',type); // this here is just for testing.
            break;
        case 3:
            inventory[inventory.length] = temp;
            break;
    }
}